package com.example.controller;

import com.example.entity.JsMind;
import com.example.service.JSMindService;
import com.example.utils.Msg;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Controller
public class JsMindController {
    @Resource
    JSMindService jsMindService;


    @ResponseBody
    @GetMapping("/JsMind/Create")
    public JsMind GetJSMind(@RequestParam("book_name") String book_name){
        return jsMindService.CreateMind(book_name);
    }

    @ResponseBody
    @PostMapping("/JsMind/AddBook")
    public Msg AddBook (MultipartFile file) throws IOException {

        String file_name = file.getOriginalFilename();
        String book_name= file_name.substring(0,file_name.lastIndexOf('.'));
        if(jsMindService.IfBookExist(book_name)){
            return Msg.fail();
        }

       if(!jsMindService.AddBook(file)){
           return Msg.fail();
       }

       return Msg.success();

    }
}
